;(function () {
	new Vue({
		el: '#finicialDocTypeConfig',
		data: {
			accountingArr: [],
			accountingSelectId: '',
			voucherTypeArr: [],
			ops: {
			  bar: {
			    background: '#c1c1c1'
			  }
			}
		},
		methods: {
			//获取账套列表
			accountingsDt () {
				hlhttp.get({
					url: 'acct/accountings',
					success: (data) => {
						if (!data.length) return;
						this.accountingArr = data;
						this.accountingSelectId = sessionStorage.getItem("accountId");
						this.voucherTypeDt()
					}
				})
			},
			//选择账套
			selectAccounting (item) {
				this.accountingSelectId = item.id;
				sessionStorage.setItem("accountId", this.accountingSelectId);
				this.voucherTypeDt()
			},
			//获取凭证类型列表
			voucherTypeDt () {
				hlhttp.get({
					url: 'acct/voucher_types',
					data: {accounting_id: this.accountingSelectId},
					success: (data) => {
						data.map(item => {
							item.nameFlag = false
							item.memoFlag = false
						})
						this.voucherTypeArr = data;
					}
				})
			},
			//新增一列凭证
			add () {
				let flag = false;
				this.voucherTypeArr.map(item => {
					if (item.nameFlag || item.memoFlag) {
						flag = true;
					}
				})
				if (flag) {
					hl.toast('请先完成当前操作')
					return
				}
				this.voucherTypeArr.push({
					id: '',
					name: '',
					memo: '',
					status: '',
					nameFlag: true,
					memoFlag: true
				})
			},
			//保存/新增类型
			saveAddType (item, i) {
				if (!item.name) {
					hl.toast('类型不能为空')
					return
				}
				this.voucherTypeArr[i].nameFlag = false;
				if (!item.nameFlag && !item.memoFlag) {
					if (!item.id) {
						this.post(item)
					}
					if (item.id) {
						this.put(item)
					}
				}
			},
			saveAddMemo (item, i) {
				if (!item.name) {
					hl.toast('类型不能为空')
					return
				}
				this.voucherTypeArr[i].memoFlag = false;
				if (!item.nameFlag && !item.memoFlag) {
					if (!item.id) {
						this.post(item)
					}
					if (item.id) {
						this.put(item)
					}
				}
			},
			//post
			post (item) {
				hlhttp.post({
					url: 'acct/voucher_type',
					data: {
						accounting_id: this.accountingSelectId,
						name: item.name,
						memo: item.memo ? item.memo : '#'
					},
					success: () => {
						hl.toast('新增成功')
						this.voucherTypeDt()
					}
				})
			},
			//put
			put (item) {
				hlhttp.put({
					url: 'acct/voucher_type',
					data: {
						id: item.id,
						name: item.name,
						memo: item.memo ? item.memo : '#'
					},
					success: () => {
						hl.toast('编辑成功')
						this.voucherTypeDt()
					}
				})
			},
			// 编辑显示
			editDisplay (i, type) {
				this.voucherTypeArr[i].nameFlag = true;
				this.voucherTypeArr[i].memoFlag = true;
			},
			//启用停用
			statusSwitch (item) {
				hlhttp.put({
					url: 'acct/voucher_type/_status',
					data: {
						id: item.id,
						opt: item.status == 'Y' ? 'N' : 'Y'
					},
					success: () => {
						hl.toast('操作成功')
						this.voucherTypeDt()
					}
				})
			},
			//删除
			del (item) {
				hllayer.confirm('确认删除凭证？删除之后不可恢复', () => {
				  hlhttp.delete({
				  	url: 'acct/voucher_type?id=' + item.id,
				  	success: () => {
				  		hl.toast('操作成功')
				  		this.voucherTypeDt()
				  	}
				  })
				})
			},
			//取消删除
			cancel (item, i) {
				if (!item.id) {
					this.voucherTypeArr.splice(i, 1)
				}
				if (item.id) {
					this.voucherTypeDt()
				}
			}
		},
		mounted () {
			this.accountingsDt()
		}
	})
} ())
